/**
 * Copyright (c) 2010 Jesco Freund.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of Backtory nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *  $Id$
 */


#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include <openssl/err.h>
#include <openssl/evp.h>

#include "_digest.h"
#include "_file64.h"


const EVP_MD *
__yar_digest_get_md(uint16_t algorithm)
{
	switch (algorithm)
	{
		case LIBYAR_DIGEST_MD5:
			return EVP_md5();
			break;
		case LIBYAR_DIGEST_RIPEMD160:
			return EVP_ripemd160();
			break;
		case LIBYAR_DIGEST_SHA1:
			return EVP_sha1();
			break;
		case LIBYAR_DIGEST_SHA224:
			return EVP_sha224();
			break;
		case LIBYAR_DIGEST_SHA256:
			return EVP_sha256();
			break;
		case LIBYAR_DIGEST_SHA384:
			return EVP_sha384();
			break;
		case LIBYAR_DIGEST_SHA512:
			return EVP_sha512();
			break;
		case LIBYAR_DIGEST_WHIRLPOOL:
			return EVP_whirlpool();
			break;
		default:
			return NULL;
	}
}


extern int
yar_digest_bf(uint16_t algorithm, const void *string, size_t stringlength, void *buffer, size_t len)
{
	const EVP_MD *md;
	EVP_MD_CTX ctx;
	
	if (algorithm == LIBYAR_DIGEST_NONE)
		return 0;
	
	/* select digest algorithm */
	md = __yar_digest_get_md(algorithm);
	if (NULL == md)
		return EINVAL;
	
	/* check whether result buffer is big enough for digest result */
	if (EVP_MD_size(md) > (int)len)
		return ERANGE;
	
	/* initialize digest context */
	EVP_MD_CTX_init(&ctx);
	if (EVP_DigestInit(&ctx, md) != 1)
		return ERR_get_error();
	
	/* calculate digest */
	if (EVP_DigestUpdate(&ctx, string, stringlength) != 1)
		return ERR_get_error();
	
	/* finalize digest and copy result to buffer */
	if (EVP_DigestFinal(&ctx, buffer, NULL) != 1)
		return ERR_get_error();
	
	/* clean up */
	if (EVP_MD_CTX_cleanup(&ctx) != 1)
		return ERR_get_error();
	
	return 0;
}


extern int
yar_digest_fp(uint16_t algorithm, int fd, void *buffer, size_t len)
{
	int rval = 0;
	ssize_t rbytes;
	unsigned char rbuf[LIBYAR_DIGEST_RBUFSIZE];
	const EVP_MD *md;
	EVP_MD_CTX ctx;
	
	if (algorithm == LIBYAR_DIGEST_NONE)
			return rval;
	
	/* select digest algorithm */
	md = __yar_digest_get_md(algorithm);
	if (NULL == md)
		return EINVAL;
	
	if (EVP_MD_size(md) > (int)len)
		return ERANGE;
	
	/* initialize digest context */
	EVP_MD_CTX_init(&ctx);
	if (EVP_DigestInit(&ctx, md) != 1)
		return ERR_get_error();
	
	/* move fd pointer to beginning of file */
	if (yar_file64_seek(fd, 0, SEEK_SET) < 0)
		return errno;
	
	/* read chunks from file and update digest */
	while ((rbytes = read(fd, rbuf, LIBYAR_DIGEST_RBUFSIZE)) > 0)
	{
		if (rbytes < 0)
			return errno;
		
		if (EVP_DigestUpdate(&ctx, rbuf, rbytes) != 1)
			return ERR_get_error();
	}
	
	/* finalize digest and copy result to buffer */
	if (EVP_DigestFinal(&ctx, buffer, NULL) != 1)
		return ERR_get_error();
	
	/* clean up */
	if (EVP_MD_CTX_cleanup(&ctx) != 1)
		return ERR_get_error();
	
	return rval;
}


extern int
yar_digest_fn(uint16_t algorithm, const char *filename, void *buffer, size_t len)
{
	int fd, rval;
	
	fd = yar_file64_open(filename, O_RDONLY);
	if (fd < 0)
		return errno;
	
	rval = yar_digest_fp(algorithm, fd, buffer, len);
	yar_file64_close(fd);
	
	return rval;
}
